/**
 * 人民法院拍卖网”
 *  
 */
import { Base, Item } from '../base';
class zggtzylnxx extends Base {
    constructor() {
        super();
        this.name = '辽宁司法拍卖信息';
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "lnxm";
        this.collectionShowName = "辽宁项目爬虫";
        this.keyField = "title";
    }
    getItems(pageIndex: number) {
        let num = pageIndex + 1;

        let url = 'http://www1.rmfysszc.gov.cn/projects.shtml?dh=3&gpstate=1&wsbm_slt=1';
        let postData = {
            'type': 0,
            "name": '',
            "area": "",
            "state": 4,
            "time": 0,
            "time1": "",
            "time2": "",
            "money": "",
            "money1": "",
            "number": 0,
            "fid1": 95,
            "fid2": "",
            "fid3": "",
            "order": 0,
            "page": 1,
            "include": 0
        };
        this.spider.getSync(url);
        (<any>this.spider).headers = {
            Referer: "http://www1.rmfysszc.gov.cn/projects.shtml?dh=3&gpstate=1&wsbm_slt=1",//后台会根据Referer是否正确拦截恶意网站
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
            "Host": "www1.rmfysszc.gov.cn"
        }
        let ul = 'http://www1.rmfysszc.gov.cn/ProjectHandle.shtml';
        let content = this.spider.postSync(ul, postData);
        let $ = this.cheerio.load(content);
        let result: Item[] = [];
        let that = this;
        try {
            $('div[id!=""]').each(function (index, ele) {
                let item = that.buildItem();
                let id = $(ele).attr('id');
                let title = $(ele).find('a').eq(0).attr('title').trim();
                let uri = `http://www.rmfysszc.gov.cn/statichtml/rm_obj/${id}.shtml`;
                result.push(that.getExtraItem(uri, title));
            });
        }
        catch (error) {
            console.error(error);
        }
        return result;
    }
    getExtraItem(url: string, title: string) {
        let item = this.buildItem();
        try {
            (<any>this.spider).headers = {};
            let content = this.spider.getSync(url);
            (<any>this.spider) = {
                "Referer": url,
                "Host": "www1.rmfysszc.gov.cn",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"
            };
            let reg = /sourceNumber=([\d\.]*)&/;
            let num = (<any>reg.exec(content))[1];
            let reg1 = /&oid=([\d\.]*)';/;
            let num2 = (<any>reg.exec(content))[1];
            let u = `http://www1.rmfysszc.gov.cn/GetHtml.aspx?jsoncallback=jQuery111106021716076490331_1517051736343&url=https%3A%2F%2Fauction.rmfysszc.gov.cn%2FBidResults.aspx%3Fsource%3D39d6dabb-991d-45ee-8b43-0f3625420986%26sourceNumber%3D${num}%26time%3D2018-01-10%26oid%3D${num2}&_=1517051736344`;

            let content1 = this.spider.getSync('http://www1.rmfysszc.gov.cn/GetHtml.aspx?jsoncallback=jQuery1111023984241565785247_1517057394465&url=https%3A%2F%2Fauction.rmfysszc.gov.cn%2FBidResults.aspx%3Fsource%3D39d6dabb-991d-45ee-8b43-0f3625420986%26sourceNumber%3D10000120180000009264%26time%3D2018-01-10%26oid%3D11067&_=1517057394466');
            //return false;

            let $ = this.cheerio.load(content);
            item.title = title;
            (<any>item).url = url;
            $('#wind table tr').each(function (index, ele) {
                console.log($(ele).find('td').eq(0).text().trim());
                (<any>item)[$(ele).find('td').eq(0).text().trim()] = $(ele).find('td').eq(1).text().trim();
            });
            $('#wind div.div1').each(function (index, ele) {
                if (index == 0) {
                    (<any>item)['竞拍明细'] = $(ele).text().trim();
                }
                else {
                    console.log($(ele).text().trim().replace(/\"/g, '').substring(0, 4));
                    (<any>item)[$(ele).text().trim().replace(/\"/g, '').substring(0, 4)] = $(ele).text().trim().replace(/\"/g, '').substring(4);
                }
            });
        }
        catch (error) {
        }
        return item;


    }
    getPageCount(): number {
        return 6;
    }
}
module.exports = new zggtzylnxx();